package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.DormAddDTO;
import com.oa.admin.dto.DormApprovalDTO;
import com.oa.admin.dto.DormConditionDTO;
import com.oa.admin.entity.Dorm;
import com.oa.admin.service.IDormService;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/dorm")
@Api(description = "宿舍申请表模块")
public class DormController extends BaseController {

    private final IDormService dormService;

    @Autowired
    public DormController(IDormService dormService) {
        this.dormService = dormService;
    }

    /**
     * 查询列表
     *
     * @param dormConditionDTO DormConditionDTO
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public Resp findList(@ApiParam("查询条件") DormConditionDTO dormConditionDTO) {
        dormConditionDTO.setUserId(getLoginUserId());
        return new Resp().success(dormService.findDormListByCondition(dormConditionDTO));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp findById(@ApiParam(value = "id", required = true) @PathVariable String id) throws DataIsEmptyException {
        return dormService.findDormDetailsById(id);
    }


    /**
     * 新增
     *
     * @param dormAddDTO DormAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "宿舍申请表对象") @RequestBody(required = false) DormAddDTO dormAddDTO) throws BusinessException, DataIsEmptyException {
        return dormService.addDorm(getLoginUserId(), dormAddDTO);
    }

    @PutMapping("/audit")
    @CommonController
    @Description("审批")
    @ApiOperation(value = "审批")
    public Resp audit(@ApiParam("宿舍审批对象") @RequestBody DormApprovalDTO dormApprovalDTO) throws BusinessException, DataIsEmptyException {
        return dormService.dormApproval(dormApprovalDTO);
    }

    /**
     * 根据Id修改
     *
     * @param dorm Dorm
     * @return Resp
     */
//    @PutMapping("/update")
//    @CommonController
//    @Description("根据id修改")
//    @ApiOperation(value = "根据id修改")
//    public Resp update(@ApiParam(value = "宿舍申请表对象") @RequestBody(required = false) Dorm dorm) {
//        if (dormService.updateById(dorm)) {
//            return new Resp().success();
//        } else {
//            return new Resp().failure();
//        }
//    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
//    @DeleteMapping("/{id}")
//    @CommonController
//    @Description("根据id删除")
//    @ApiOperation(value = "根据id删除")
//    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
//        Dorm dorm = dormService.selectById(id);
//        if (dorm != null && dorm.getIsDeleted() == 2) {
//            dorm.setIsDeleted(1);
//            if (dormService.updateById(dorm)) {
//                return new Resp().success();
//            }
//            return new Resp().failure();
//        }
//        return new Resp().failure();
//    }

}
